package com.xiye.common.session.redis.impl;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheException;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.util.Destroyable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xiye.common.session.redis.RedisCache;

/**
 * @author 西野
 * @version V1.0
 * @title RedisCacheManager.java
 * @package com.xiye.common.session.redis.impl
 * @description redis缓存实现
 * @date 2018-07-13
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public class RedisCacheManager implements CacheManager, Destroyable {
    private static final Logger logger = LoggerFactory.getLogger(RedisCacheManager.class);

    // fast lookup by name map
    private final ConcurrentMap<String, Cache> caches = new ConcurrentHashMap<String, Cache>();

    private RedisManager redisManager;

    /**
     * The Redis key prefix for caches
     */
    /*@Value("${redis.keyPrefix}")*/
    private String keyPrefix = "shiro_session:";


    /**
     * Returns the Redis session keys prefix.
     *
     * @return The prefix
     */
    public String getKeyPrefix() {
        return keyPrefix;
    }

    /**
     * Sets the Redis sessions key prefix.
     *
     * @param keyPrefix The prefix
     */
    public void setKeyPrefix(String keyPrefix) {
        this.keyPrefix = keyPrefix;
    }

    @Override
    public <K, V> Cache<K, V> getCache(String name) throws CacheException {
        logger.debug("获取名称为: " + name + " 的RedisCache实例");
        Cache c = caches.get(name);
        if (c == null) {
            // initialize the Redis manager instance
            redisManager.init();
            /*Properties pros = RedisManager.getPprVue();
            keyPrefix = pros.getProperty("redis.keyPrefix"); */
            // create a new cache instance
            c = new RedisCache<K, V>(redisManager, keyPrefix);
            // add it to the cache collection
            caches.put(name, c);
        }
        return c;
    }

    public RedisManager getRedisManager() {
        return redisManager;
    }

    public void setRedisManager(RedisManager redisManager) {
        this.redisManager = redisManager;
    }

    @Override
    public void destroy() throws Exception {
        this.redisManager.destroy();
    }
}
